//
//  QuranApi.m
//  Api
//
//  Created by CPX on 2018/6/7.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "QuranApi.h"

/**
 古兰经接口
 @author yzl
 */
@implementation QuranApi

+ (void)downloadMp3:(NSString *)url savePath:(NSString *)savePath obj:(id)obj success:(Success)success failure:(Failure)failure{
    [NetworkManager DownloadFile:url progress:^(NSProgress *downloadProgress) {
        
    } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        return [NSURL fileURLWithPath:savePath];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        if (!error) {
            success(obj);
        }
        else{
            failure(error);
        }
    } name:@"g1"];
}

+ (void)getQuranBackgroundWithSuccess:(Success)success failure:(Failure)failure{
    NSString *url = [NSString stringWithFormat:@"%@%@",RootURL,QuranBackgroundPath];
    
    [NetworkManager GETWithURL:url header:[self header] params:nil success:^(id response) {
        
        if (response[@"code"] && [response[@"code"] integerValue] == 0) {
            success(response[@"data"]);
        }
        else{
            NSData *d = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"quranbg" ofType:@"json"]];
            id data = [NSJSONSerialization JSONObjectWithData:d options:NSJSONReadingAllowFragments error:nil];
            success(data);
//            failure(nil);
        }
        
    } failure:^(NSError *error) {
        NSData *d = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"quranbg" ofType:@"json"]];
        id data = [NSJSONSerialization JSONObjectWithData:d options:NSJSONReadingAllowFragments error:nil];
        success(data);
//        failure(error);
    }];
}

@end
